package com.fancy.learncenter.ui.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.fancy.learncenter.R;
import com.fancy.learncenter.bean.BaseBean;
import com.fancy.learncenter.bean.CartoonTeacherListSutendSpeakDataBean;
import com.fancy.learncenter.net.CustomNetCallBack;
import com.fancy.learncenter.net.CustomNetSubscriber;
import com.fancy.learncenter.net.HttpMehtod;
import com.fancy.learncenter.ui.activity.base.BaseActivity;
import com.fancy.learncenter.ui.adapter.CartoonTeacherEveryDayReportAdapter;
import com.fancy.learncenter.ui.adapter.TeacherReportDetailAdapter;
import com.fancy.learncenter.utils.NetUtil;
import com.fancy.learncenter.utils.Utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.ResponseBody;
import retrofit2.Response;

public class CartoonTeacherEveryDayActivity extends BaseActivity {
    String classId;
    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;


    private CartoonTeacherEveryDayReportAdapter everyDayReportAdapter;

    ArrayList<CartoonTeacherListSutendSpeakDataBean.ResultBean> itemDatas;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cartoon_teacher_every_day, Status.STATUS);
        ButterKnife.bind(this);
        initToolbar("每日跟读");

        classId = getIntent().getStringExtra(CartoonTeacherClassDetailActivity.CLASS_ID_KEY);
        itemDatas = new ArrayList<>();

        everyDayReportAdapter = new CartoonTeacherEveryDayReportAdapter(this, itemDatas, classId);
        everyDayReportAdapter.setUrgeCallBackl(new TeacherReportDetailAdapter.UrgeCallBack() {
            @Override
            public void urgeCallBackClick(String studentHomeworkId, int position) {
                urgeStudentSpeak(studentHomeworkId);

                everyDayReportAdapter.itemDatas.get(position).setNodifyState("1");

                everyDayReportAdapter.notifyItemChanged(position);

            }
        });

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);

        recyclerView.setAdapter(everyDayReportAdapter);
        getTeacherList();
    }


    private void getTeacherList() {
        Map<String, Object> map = new HashMap<>();
        map.put("classId", classId);

        HttpMehtod.getInstance().listStudentSpeak(map, new CustomNetSubscriber(this, true) {
            @Override
            public void onNext(Response<ResponseBody> responseBodyResponse) {

                BaseBean<CartoonTeacherListSutendSpeakDataBean> baseBean = Utils.fromJsonObject(NetUtil.getResResult(responseBodyResponse), CartoonTeacherListSutendSpeakDataBean.class);
                CustomNetCallBack callBack = new CustomNetCallBack(getContext(), baseBean);
                callBack.setCallBack(new CustomNetCallBack.NetCallBack<CartoonTeacherListSutendSpeakDataBean>() {
                    @Override
                    public void success(CartoonTeacherListSutendSpeakDataBean dataBean) {
                        everyDayReportAdapter.addAll(dataBean.getResult());
                    }

                    @Override
                    public void fail(String errorCode) {


                    }
                }).dealData();
            }

        });
    }

    private void urgeStudentSpeak(String studentId) {
        Map<String, Object> map = new HashMap<>();
        map.put("studentId", studentId);
        map.put("classId", classId);
        map.put("msgType", "5");

        HttpMehtod.getInstance().urgeStudentSpeak(map, new CustomNetSubscriber(this, true) {
            @Override
            public void onNext(Response<ResponseBody> responseBodyResponse) {

                BaseBean<CartoonTeacherListSutendSpeakDataBean> baseBean = Utils.fromJsonObject(NetUtil.getResResult(responseBodyResponse), CartoonTeacherListSutendSpeakDataBean.class);
                CustomNetCallBack callBack = new CustomNetCallBack(getContext(), baseBean);
                callBack.setCallBack(new CustomNetCallBack.NetCallBack<CartoonTeacherListSutendSpeakDataBean>() {
                    @Override
                    public void success(CartoonTeacherListSutendSpeakDataBean dataBean) {
                        everyDayReportAdapter.addAll(dataBean.getResult());
                    }

                    @Override
                    public void fail(String errorCode) {


                    }
                }).dealData();
            }

        });
    }

}
